package com.lishangshan.oas_system.utils;

import com.lishangshan.oas_system.service.process.ProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

@Controller
public class FileUpload {
    // 定义本地上传图片 路径
    @Value("${img.rootpath}")
    private String imgpath;
    @Value("${file.rootpath}")
    private String filpath;

/*    @Value("${file.rootpath}")
    private static String filePath;*/

    /**
     * 单一图片上传
     *
     * @param uploadfile
     * @param request
     * @return
     * @throws IOException
     */
    public String imgUpload(MultipartFile uploadfile, HttpServletRequest request) throws IOException {
        System.out.println("img.rootpath = " + imgpath);
        //指定上传路径
        String newFileName = null;
        File file = new File(imgpath);
        //指定上传后的文件名称
        newFileName = System.currentTimeMillis() + uploadfile.getOriginalFilename();
        //文件上传
        uploadfile.transferTo(new File(file, newFileName));
        return "storage/images/" + newFileName;
    }

/*    public static String fileDownload(String filePath, String fileName, HttpServletResponse response) throws IOException {
        String realFilePath = filePath + "uploadFiles";
        File file = new File(realFilePath);
        if (file.exists()) {
            int fileLength = (int) file.length();
            if (fileLength != 0) {
                String suffix = filePath.substring(filePath.lastIndexOf("."));
                String realFileName = fileName + suffix;
                realFileName = new String(realFileName.getBytes(), "ISO-8859-1");
                response.reset();
                response.setContentType("application/octet-stream");

                response.setHeader("Content-disposition", "attachment;filename=" + realFileName);
                response.setContentLength(fileLength);
                InputStream is = new FileInputStream(realFilePath);
                byte[] buffer = new byte[1024];
                ServletOutputStream fos = response.getOutputStream();
                int len = 0;
                while ((len = is.read(buffer)) != -1) {
                    fos.write(buffer, 0, len);
                }
                is.close();
                fos.flush();
                fos.close();
            }
        }
        return realFilePath;
    }*/

    /**
     * 文件下载 封装
     * 普通字节流下载
     *
     * @param fileName
     * @param response
     * @return
     * @throws IOException
     */
    public String fileDownload(String fileName, HttpServletResponse response) throws IOException {
        String realFilePath = filpath + fileName;
        File file = new File(realFilePath);
        if (file.exists()) {
            long fileLength = file.length();
            if (fileLength != 0) {
                String realFileName = realFilePath.substring(realFilePath.lastIndexOf("/"));
                System.out.println(realFileName);
                realFileName = new String(realFileName.getBytes(), "ISO-8859-1");
                response.reset();
                response.setContentType("application/octet-stream");
                response.setHeader("Content-disposition", "attachment;filename=" + realFileName);
                response.setContentLength((int) fileLength);
                InputStream is = new FileInputStream(realFilePath);
                byte[] buffer = new byte[1024];
                ServletOutputStream fos = response.getOutputStream();
                int len = 0;
                while ((len = is.read(buffer)) != -1) {
                    fos.write(buffer, 0, len);
                }
                is.close();
                fos.flush();
                fos.close();
            }
        }
        return realFilePath;
    }

    @Autowired
    private ProcessService processService;

    public String downFile(String fileName, HttpServletResponse response) {
        try {
            String realFilePath = filpath + fileName;
            File file = new File(realFilePath);// 文件 路径
            response.setContentLength((int) file.length()); // 文件 大小
            response.setContentType("application/octet-stream"); // 下载类型
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("UTF-8"), "ISO8859-1")); // 设置编码
            processService.writefile(response, file); // 调用写文件方法开始下载
        } catch (Exception e) {
            e.printStackTrace();
        }
        return filpath;
    }
}